package com.hms.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 * 所在地信息<br>
 * 类详细说明.
 * <p>
 * Copyright: Copyright (c) 2013-4-13 上午11:08:03
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * 
 * @author wangpz@c-platform.com
 * @version 1.0.0
 */
@Entity
@Table(name = "td_location", uniqueConstraints = { @UniqueConstraint(columnNames = { "areaName", "parent_id" }) })
public class TdLocation extends BaseEntity {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	// 分割符
	public static final String PATH_SEPARATOR = ",";

	private String areaName;

	private String path;

	private Boolean isImportantCity;

	private TdLocation parent;

	private Set<TdLocation> children;

	/**
	 * 城市名
	 * 
	 * @return
	 */
	@Column(name = "areaName", nullable = false)
	public String getAreaName() {
		return areaName;
	}

	/**
	 * 城市名
	 * 
	 * @return
	 */
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	/**
	 * 路径
	 * 
	 * @return
	 */
	@Column(name = "path", nullable = false)
	public String getPath() {
		return path;
	}

	/**
	 * 路径
	 * 
	 * @return
	 */
	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * 是否为重要城市
	 * 
	 * @return
	 */
	@Column(name = "isImportantCity", nullable = false)
	public Boolean getIsImportantCity() {
		return isImportantCity;
	}

	/**
	 * 是否为重要城市
	 * 
	 * @return
	 */
	public void setIsImportantCity(Boolean isImportantCity) {
		this.isImportantCity = isImportantCity;
	}

	/**
	 * 上级城市
	 * 
	 * @return
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	public TdLocation getParent() {
		return parent;
	}

	/**
	 * 上级城市
	 * 
	 * @return
	 */
	public void setParent(TdLocation parent) {
		this.parent = parent;
	}

	/**
	 * 下级城市
	 * 
	 * @return
	 */
	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.DELETE })
	@OrderBy("areaName asc")
	public Set<TdLocation> getChildren() {
		return children;
	}

	/**
	 * 下级城市
	 * 
	 * @return
	 */
	public void setChildren(Set<TdLocation> children) {
		this.children = children;
	}

	// 获取分类层级（顶级分类：0）
	@Transient
	public Integer getLevel() {
		return path.split(PATH_SEPARATOR).length - 1;
	}
}
